package com.jiazhong.basic.atm.db;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import lombok.SneakyThrows;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class DBManager {
    // 原始的写法
    @SneakyThrows
    public static Connection getConnection() {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql:///atm";
        String user = "root";
        String password = "a123456";
        return DriverManager.getConnection(url, user, password);
    }

    // 数据库连接池的写法
    @SneakyThrows
    public static Connection getDruidConnection() {
        // 加载配置文件(druid.properties)
        Properties properties = new Properties();
        InputStream in = DBManager
                .class.getClassLoader().getResourceAsStream("druid.properties");
        properties.load(in);
        // 获取数据源
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        // 获取连接
        return dataSource.getConnection();
    }
}
